<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BootstrapTag
 *
 * @author bonfiglio
 */
class BootstrapTag {

    public static $options = array(
        'fluid' => false,
        'form' => false,
        'post' => array(),
        'options' => array(),
        'errors' => array(),
        'form-type' => 'horizontal',
        'class-helper-form' => 'help-block'
    );
    public $Form;

    public function __construct($options = array()) {
        self::$options = array_merge(self::$options, $options);
        if (isset(self::$options['form'])) {
            $this->Form = new FormFields(self::$options['form'], array_merge(array('displayTag' => false, 'displayErrors' => true), self::$options['options']));
            $this->Form->setPost(self::$options['post']);
            $this->Form->setErrors(self::$options['errors']);
        }
    }

    public static function generateTab($tabs, $current = false, $navAttributs = array(), $tabAttributs = array()) {
        $return = '';
        if (is_array($tabs) && count($tabs)) {
            $nav = '';
            $tabContent = '';
            $current = ($current) ? $current : $tabs[0]['name'];
            foreach ($tabs as $tab) {
                $name = $tab['name'];

                $attributs = isset($tab['attributslink']) ? $tab['attributslink'] : array();
                $attributsLi = array();
                if ($name == $current) {
                    $attributsLi = HtmlTag::addClassAttributs('active', $attributsLi);
                }
                $attributs['href'] = '#' . $name;
                $attributs['data-toggle'] = "tab";
                $navLine = HtmlTag::a($tab['nav'], $attributs);
                $nav .= HtmlTag::li($navLine, $attributsLi);

                $attributs = isset($tab['attributsContent']) ? $tab['attributsContent'] : array();
                $attributs['id'] = $name;
                if ($name == $current) {
                    $attributs = HtmlTag::addClassAttributs('active', $attributs);
                }
                $tabContent .= HtmlTag::div($tab['content'], HtmlTag::addClassAttributs('tab-pane', $attributs));
            }
            $return .= HtmlTag::ul($nav, HtmlTag::addClassAttributs('nav-tabs', HtmlTag::addClassAttributs('nav', $navAttributs)));
            $return .= HtmlTag::div($tabContent, HtmlTag::addClassAttributs('tab-content', $tabAttributs));
        }
        return $return;
    }

    public static function generateGroupButtons($buttons, $vertical = false, $attributsBlock = array()) {
        $return = '';
        if (is_array($buttons) && count($buttons)) {
            $vertical = ($vertical) ? 'btn-group-vertical' : '';
            if (isset($buttons['html'])) {
                $return = $buttons['html'];
            } else {
                foreach ($buttons as $button) {
                    if (isset($button['html'])) {
                        $return .= $button['html'];
                    } else {
                        $attributs = isset($button['attributs']) ? $button['attributs'] : array();
                        $tag = isset($button['tag']) ? $button['tag'] : 'button';
                        $icon = isset($button['icon']) ? $button['icon'] : array();
                        $return .= self::button($button['content'], $attributs, $tag, $icon);
                    }
                }
            }

            $attributsBlock = HtmlTag::addClassAttributs('btn-group', HtmlTag::addClassAttributs($vertical, $attributsBlock));
            $return = HtmlTag::div($return, $attributsBlock);
        }
        return $return;
    }

    public static function generateProgressBar($percent, $color = 0, $label = false, $active = false, $striped = false, $attributs = array(), $precision = 2) {
        $return = '';
        if ($percent >= 0 || $percent <= 100) {
            $attributs = HtmlTag::addClassAttributs('progress', $attributs);

            $percent = round($percent, $precision);
            if ($label) {
                if (!is_string($label)) {
                    $label = vsprintf('%s', array($percent)) . '%';
                } else {
                    $label = vsprintf($label, array($percent));
                }
                $attributs = HtmlTag::addPropertyAttributs('style', 'position: relative;', $attributs);
                $label = HtmlTag::div($label, array('class' => 'bar-label', 'style' => 'position:absolute;left:0;width:100%;text-align:center;top:0px;'));
            } else {
                $label = '';
            }
            $return = HtmlTag::div('', array('class' => 'bar', 'style' => 'width:' . $percent . '%;')) . $label;
            $colors = array(
                '',
                'progress-info',
                'progress-success',
                'progress-warning',
                'progress-danger'
            );
            if ($color == 'auto') {
                if ($percent <= 33) {
                    $color = 2;
                } elseif ($percent <= 66) {
                    $color = 3;
                } else {
                    $color = 4;
                }
                $attributs = HtmlTag::addClassAttributs($colors[$color], $attributs);
            } elseif (isset($colors[$color])) {
                $attributs = HtmlTag::addClassAttributs($colors[$color], $attributs);
            }

            if ($striped) {
                $attributs = HtmlTag::addClassAttributs('progress-striped', $attributs);
            }
            if ($active) {
                $attributs = HtmlTag::addClassAttributs('active', $attributs);
            }

            $return = HtmlTag::div($return, $attributs);
        }
        return $return;
    }

    public static function generateCollapse($name, $children, $attributs = array()) {
        $return = '';
        if (is_array($children) && count($children) && strlen($name)) {
            foreach ($children as $id => $child) {
                $collapseId = is_numeric($id) ? $name . '-' . $id : $id;
                $heading = $child['heading'];
                $headingAttributs = isset($heading['attributs']) ? $heading['attributs'] : array();
                $headingAttributs = HtmlTag::addClassAttributs('accordion-toggle', $headingAttributs);
                $headingAttributs = array_merge($headingAttributs, array('data-toggle' => 'collapse', 'data-parent' => '#' . $name, 'href' => '#' . $collapseId));

                $append = isset($heading['append']) ? $heading['append'] : '';
                $heading = HtmlTag::a($heading['content'], $headingAttributs);
                $heading = HtmlTag::div($heading . $append, array('class' => 'accordion-heading'));

                $collapse = $child['collapse'];
                $collapseAttributs = isset($collapse['attributs']) ? $collapse['attributs'] : array();
                $in = isset($collapse['open']) && $collapse['open'];
                $collapseAttributs = HtmlTag::addClassAttributs('accordion-body', $collapseAttributs);
                $collapseAttributs = HtmlTag::addClassAttributs('collapse', $collapseAttributs);
                if ($in) {
                    $collapseAttributs = HtmlTag::addClassAttributs('in', $collapseAttributs);
                }
                $collapse = HtmlTag::div($collapse['content'], array('class' => 'accordion-inner'));
                $collapse = HtmlTag::div($collapse, array_merge($collapseAttributs, array('id' => $collapseId)));

                $return .= HtmlTag::div($heading . $collapse, array('class' => 'accordion-group'));
            }
            $attributs = HtmlTag::addClassAttributs('accordion', $attributs);
            $return = HtmlTag::div($return, array_merge($attributs, array('id' => $name)));
        }
        return $return;
    }

    public function generateForm($formAttributs = array(), $action = true, $submitAttributs = array()) {
        $content = '';
        $formType = (in_array(self::$options['form-type'], array('inline', 'horizontal'))) ? self::$options['form-type'] : 'horizontal';
        foreach ($this->Form->fields as $fieldName => $d) {
            $type = $this->Form->getType($fieldName);
            switch ($type) {
                case 'select':
                    $content .= $this->formControlGroupSelect($fieldName);
                    break;
                case 'radio':
                case 'checkbox':
                    $content .= $this->formControlGroupCheckboxes($fieldName);
                    break;
                case 'textarea':
                    $content .= $this->formControlGroupTextarea($fieldName);
                    break;
                default :
                    $content .= $this->formControlGroupInput($fieldName);
                    break;
            }
        }
        $formAttributs = HtmlTag::addClassAttributs('form-' . $formType, $formAttributs);
        if ($action === true) {
            $content .= self::formActions(null, $submitAttributs);
        } elseif (is_string($action) && strlen($action)) {
            $content .= $action;
        }
        if (!isset($formAttributs['method'])) {
            $formAttributs['method'] = 'post';
        }
        $content = HtmlTag::form($content, $formAttributs);
        return $content;
    }

    public static function container($content = '', $attributs = array(), $tag = 'div') {
        $class = 'container';
        if (self::$options['fluid']) {
            $class .= '-fluid';
        }
        $attributs = HtmlTag::addClassAttributs($class, $attributs);
        return HtmlTag::tag($tag, $content, $attributs);
    }

    public static function row($content = '', $attributs = array(), $tag = 'div') {
        $class = 'row';
        if (self::$options['fluid']) {
            $class .= '-fluid';
        }
        $attributs = HtmlTag::addClassAttributs($class, $attributs);
        return HtmlTag::tag($tag, $content, $attributs);
    }

    public static function span($unit, $content = '', $attributs = array(), $tag = 'div') {
        $class = 'span';
        $unit = (int) $unit;
        if ($unit > 0) {
            $class .= $unit;
        }
        $attributs = HtmlTag::addClassAttributs($class, $attributs);
        return HtmlTag::tag($tag, $content, $attributs);
    }

    public static function submitButton($content = '', $attributs = array(), $tag = 'button', $icon = false) {
        $attributs = HtmlTag::addPropertyAttributs('type', 'submit', $attributs);
        return BootstrapTag::button($content, $attributs, $tag, $icon);
    }

    public static function button($content = '', $attributs = array(), $tag = 'button', $icon = false) {
        $attributs = HtmlTag::addClassAttributs('btn', $attributs);
        if ($icon && strlen($icon)) {
            $content = HtmlTag::i('', array('class' => $icon)) . '&nbsp;' . $content;
        }
        return HtmlTag::tag($tag, $content, $attributs);
    }

    public function formControlGroupSelect($fieldName, $attrControlGroup = array(), $selectAttr = array()) {
        $html = '';
        $html .= $this->Form->getLabelTag($fieldName, array('class' => 'control-label'));
        $element = $this->Form->getSelectTag($fieldName, $selectAttr);
        $html .= HtmlTag::div($element, array('class' => 'controls'));
        return Htmltag::div($html, HtmlTag::addClassAttributs('control-group control-' . $fieldName, $attrControlGroup));
    }

    public function formControlGroupInput($fieldName, $attrControlGroup = array(), $inputAttr = array(), $prepend = '', $append = '') {
        $html = $element = '';
        if (isset($this->Form->fields[$fieldName]['attributs'])) {
            $inputAttr = array_merge($this->Form->fields[$fieldName]['attributs'], $inputAttr);
        }
        if ($this->Form->getType($fieldName) == 'hidden') {
            return $this->Form->getInputTag($fieldName);
        } else {
            $html .= $this->Form->getLabelTag($fieldName, array('class' => 'control-label'));
            $blockAttributs = array('class' => 'controls');
            $addonAttributs = array();
            $input = $this->Form->getInputTag($fieldName, $inputAttr);
            if (strlen($prepend)) {
                $prepend = HtmlTag::span($prepend, array('class' => 'add-on'));
                $addonAttributs = HtmlTag::addClassAttributs('input-prepend', $addonAttributs);
            }
            if (strlen($append)) {
                $append = HtmlTag::span($append, array('class' => 'add-on'));
                $addonAttributs = HtmlTag::addClassAttributs('input-append', $addonAttributs);
            }
            if (count($addonAttributs)) {
                $input = HtmlTag::div($prepend . $input . $append, $addonAttributs);
            }
            $element .= $input;
            if ($this->Form->hasError($fieldName)) {
                $element .= HtmlTag::div($this->Form->errors[$fieldName], array('class' => self::$options['class-helper-form']));
            }
            $html .= HtmlTag::div($element, $blockAttributs);
            $attrControlGroup = HtmlTag::addClassAttributs('control-group', $attrControlGroup);
            $attrControlGroup = $this->Form->getErrorClass($fieldName, $attrControlGroup);
            return Htmltag::div($html, $attrControlGroup);
        }
    }

    public function formControlGroupCheckboxes($fieldName, $attrControlGroup = array(), $inputAttr = array(), $inline = true) {
        $inline = ($inline) ? 'inline' : '';
        $labelAttr = array('class' => array($this->Form->getType($fieldName), $inline));

        $element = $this->Form->getInputCheckboxes($fieldName, $inputAttr, $labelAttr);
        $label = $this->Form->getLabelTag($fieldName, array('class' => 'control-label'));

        return $this->formControlPattern($label, $element, $attrControlGroup);
    }

    public function formControlPattern($label, $element, $attrControlGroup = array()) {
        $html = $label . HtmlTag::div($element, array('class' => 'controls'));
        $attrControlGroup = HtmlTag::addClassAttributs('control-group', $attrControlGroup);
        return Htmltag::div($html, $attrControlGroup);
    }

    public function formControlGroupTextarea($fieldName, $attrControlGroup = array(), $inputAttr = array()) {
        $html = '';
        $html .= $this->Form->getLabelTag($fieldName, array('class' => 'control-label'));
        $element = $this->Form->getTextareaTag($fieldName, $inputAttr);
        $html .= HtmlTag::div($element, array('class' => 'controls'));
        return Htmltag::div($html, HtmlTag::addClassAttributs('control-group', $attrControlGroup));
    }

    public static function alert($content, $alertLevel = null, $attributs = array(), $canBeClosed = false) {
        $attributs = HtmlTag::addPropertyAttributs('class', 'alert', $attributs);
        if (in_array($alertLevel, array('alert-error', 'alert-success', 'alert-info', 'alert-block'))) {
            $attributs = HtmlTag::addPropertyAttributs('class', $alertLevel, $attributs);
        }
        if ($canBeClosed) {
            $content = HtmlTag::a('&times;', array('href' => '#', 'class' => 'close', 'data-dismiss' => 'alert')) . $content;
        }
        return Htmltag::div($content, $attributs);
    }

    public static function formActions($labelSubmit = null, $attrSubmit = array(), $labelCancel = null, $attrCancel = array(), $attrBlock = array()) {
        $content = '';

        $labelSubmit = (is_null($labelSubmit)) ? __txt("sauvegarder") : $labelSubmit;
        if (!isset($attrSubmit['type'])) {
            $attrSubmit = HtmlTag::addPropertyAttributs('type', 'submit', $attrSubmit);
        }
        $attrSubmit = HtmlTag::addClassAttributs('btn-primary', $attrSubmit);
        $content .= self::button($labelSubmit, $attrSubmit);

        $labelCancel = (is_null($labelCancel)) ? __txt("annuler") : $labelCancel;
        $attrCancel = HtmlTag::addPropertyAttributs('type', 'reset', $attrCancel);
        $content .= self::button($labelCancel, $attrCancel);

        $attrBlock = HtmlTag::addClassAttributs('form-actions', $attrBlock);
        $content = HtmlTag::div($content, $attrBlock);
        return $content;
    }

    public static function generateTable($body, $head = array(), $tableAttr = array()) {
        $return = '';
        $tr = '';
        if (count($head)) {
            foreach ($head as $content => $attr) {
                if (!is_array($attr)) {
                    $content = $attr;
                    $attr = array();
                }
                $tr .= HtmlTag::th($content, $attr);
            }
            $return .= HtmlTag::thead(HtmlTag::tr($tr));
        }

        $tbody = '';
        if (count($body)) {
            foreach ($body as $line) {
                $tr = '';
                foreach ($line as $cell) {
                    $tr .= HtmlTag::td($cell);
                }
                $tbody .= HtmlTag::tr($tr);
            }
        }
        $return .= HtmlTag::tbody($tbody);
        return self::table($return, $tableAttr);
    }

    public static function table($content, $tableAttr = array()) {
        $tableAttr = HtmlTag::addClassAttributs('table', $tableAttr);
        return HtmlTag::table($content, $tableAttr);
    }

    public static function modal($id, $body = '', $head = '', $hide = true, $action = '', $close = false) {
        $hide = ($hide) ? 'hide' : '';
        $return = '';
        //head
        $head = HtmlTag::button('&times;', array('type' => 'button', 'class' => 'close', 'data-dismiss' => 'modal', 'aria-hidden' => 'true')) . HtmlTag::h3($head);
        $head = HtmlTag::div($head, array('class' => 'modal-header'));

        //body
        $body = HtmlTag::div($body, array('class' => 'modal-body'));

        //actions
        $close = ($close) ? HtmlTag::button($close, array('class' => 'btn', 'data-dismiss' => 'modal', 'aria-hidden' => 'true')) : '';
        $footer = HtmlTag::div($close . $action, array('class' => 'modal-footer'));
        $return = $head . $body . $footer;
        return HtmlTag::div($return, array('class' => array('modal', 'fade', $hide), 'id' => $id, 'tabindex' => -1));
    }

    public static function divider() {
        return HtmlTag::hr(); //HtmlTag::div('',array('class'=>"divider"));
    }

}

?>
